<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="loginVo.manager" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="loginVo.manager.account" placeholder="账户">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="loginVo.manager.password"
                        @keyup.enter.native="useVerify()">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>

                <!-- 按钮 -->
                <div class="login-btn">
                    <el-button type="primary" @click="useVerify()">登录</el-button>
                </div>
                <p class="login-tips" style="text-align:center">为保证学生安全<br>学校教师登录请向本校管理员申请账号密码</p>
            </el-form>

            <Verify @success="capctchaCheckSuccess" :mode="'pop'" :captchaType="'blockPuzzle'"
                :imgSize="{ width: '330px', height: '155px' }" ref="verify"></Verify>

        </div>
    </div>
</template>

<script>
import Verify from "@/components/verifition/Verify";

export default {
    components: { Verify },
    data: function () {
        return {
            loginVo: {
                manager: {
                    account: 'super',
                    password: '123'
                },
                captcha: ''
            },
            rules: {
                account: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
            },
        };
    },
    methods: {
        submitForm() {
            this.$axios.post("/auth/admin/login", this.loginVo)
                .then((r) => {
                    console.log(r);
                    if (r.data.code == '200') {
                        localStorage.setItem('token', r.headers.token);
                        localStorage.setItem('username', r.data.data.account);
                        localStorage.setItem('userid', r.data.data.id);
                        this.$message.success('登录成功');
                        setTimeout(() => {
                            this.$router.push('/');
                        }, 1000)
                        return true;
                    } else {
                        this.$message.error(r.data.message);
                    }
                });
        },
        capctchaCheckSuccess(params) {
            console.log("成功，下面是二次验证");
            this.loginVo.captcha = params.captchaVerification;
            // params 返回的二次验证参数, 和登录参数一起回传给登录接口，方便后台进行二次验证
            this.submitForm();
        },
        useVerify() {
            this.$refs.verify.show();
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}

.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}

.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}

.ms-content {
    padding: 30px 30px;
}

.login-btn {
    text-align: center;
}

.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}

.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>